package com.qst.service.ssm;

import com.qst.ExamException;
import com.qst.mapper.UserMapper;
import com.qst.entity.User;
import com.qst.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
@Service
public class UserServiceSsmImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    public User login(String login, String password) {
        User user = userMapper.findByLogin(login);
        String msg = null;
        if (user == null) {
            msg = "用户不存在";
        } else if (user.getStatus() != 1) {
            msg = "账户已被禁用，不能登录";
        } else if (!user.getPasswd().equals(password)) {
            msg = "密码错误";
        }
        if (msg != null) {
            throw new ExamException(msg);
        }
        userMapper.updateLastLogin(user.getId(), new Timestamp(System.currentTimeMillis()));
        return user;
    }

    @Override
    public void changePassword(Integer id, String oldPassword, String newPassword) {
        User user = userMapper.findById(id);
        if (!user.getPasswd().equals(oldPassword)) {
            throw new ExamException("原密码错误");
        }
        userMapper.updatePassword(id, newPassword);
    }


}
